package com.ytjj.qmyx.admin.controller.exportInterface;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.service.adapay.AdaPayService;
import com.ytjj.qmyx.admin.model.request.SupplyChainBrandRequest;
import com.ytjj.qmyx.admin.model.request.exportInterface.NotifyOrderInfoRequest;
import com.ytjj.qmyx.admin.model.request.exportInterface.SupplyMessageRequest;
import com.ytjj.qmyx.admin.model.request.goods.GoodsRequest;
import com.ytjj.qmyx.admin.model.request.goods.UpdateOrderExpressRequest;
import com.ytjj.qmyx.admin.service.ReceiveSupplyChainMsgService;
import com.ytjj.qmyx.admin.service.ReturnRegisterService;
import com.ytjj.qmyx.mall.model.ProductSpec;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 提供给供应链回调相关接口
 */
@RestController
@RequestMapping("/callback/app")
@Slf4j
public class ReceiveSupplyChainMsgController {

    @Autowired
    private ReceiveSupplyChainMsgService receiveSupplyChainMsgService;
    @Autowired
    private AdaPayService adaPayService;
    @Autowired
    private ReturnRegisterService returnRegisterService;

    /**
     * 通知销售渠道更新商品
     * @return
     */
    @PostMapping("/notifyProductUpdate")
    public CommonResult notifyProductUpdate(@RequestBody SupplyMessageRequest request){
        log.info("===notifyProductUpdate==={},{},{}", request.getType(), request.getMsg(), request.getProductId());
        receiveSupplyChainMsgService.notifyProductUpdate(request.getType(), request.getMsg(), request.getProductId());
        return CommonResult.success();
    }

    /**
     * 查询未读消息列表
     * @param status 1-查询已读 0-查询未读
     * @return
     */
    @GetMapping("/queryMessage")
    public CommonResult queryMessage(@RequestParam(value = "status", required = false) Integer status) {
        return CommonResult.success(receiveSupplyChainMsgService.queryMsgList(null == status ? 0 : status));
    }

    /**
     * 查看详情
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public CommonResult findById(@RequestParam(value = "id") Integer id) {
        return CommonResult.success(receiveSupplyChainMsgService.findById(id));
    }

    /**
     * 通知销售渠道更新订单状态
     * @return
     */
    @PostMapping("/notifyOrderUpdate")
    public CommonResult notifyOrderUpdate(@RequestBody NotifyOrderInfoRequest  request){
        Assert.isTrue(CollectionUtils.isNotEmpty(request.getChildOrderNo()),"订单号不能为空");
        Assert.isTrue(StringUtils.isNotBlank(request.getStatus()),"status不能为空");
        receiveSupplyChainMsgService.notifyOrderUpdate(request);
        return CommonResult.success();
    }


    /**
     * 供应链新增品牌或者更新品牌时,更新销售端品牌
     */
    @PostMapping("/addOrUpdateBrand")
    public CommonResult addOrUpdateBrand(@RequestBody SupplyChainBrandRequest request){
        receiveSupplyChainMsgService.addOrUpdateBrand(request);
        return CommonResult.success();
    }

    /**
     * 供应链修改了供货价，实时同步销售端
     */
    @PostMapping("/updateChannelProductSpecSupplyPrice")
    public CommonResult updateChannelProductSpecSupplyPrice(@RequestBody List<ProductSpec> request){
        receiveSupplyChainMsgService.updateChannelProductSpecSupplyPrice(request);
        return CommonResult.success();
    }

    /**
     * 供应链修改商品分类，实时同步销售端
     */
    @PostMapping("/updateChannelProductCategory")
    public CommonResult updateChannelProductCategory(@RequestBody GoodsRequest goodsRequest){
        receiveSupplyChainMsgService.updateChannelProductCategory(goodsRequest);
        return CommonResult.success();
    }

    /**
     * 供应商促使销售端更新订单快递单信息和订单信息
     * @param request
     * @return
     */
    @PostMapping("/updateOrderExpressRpc")
    public CommonResult updateOrderExpress(@RequestBody UpdateOrderExpressRequest request) {
        int result = receiveSupplyChainMsgService.updateOrderExpress(request);
        return result > 0 ? CommonResult.success() : CommonResult.failed();
    }
}
